<html>
<head><meta charset="utf-8"><title>Adding `&amp;self` variants of read/write to `unix::fs::FileExt` · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20.60.26self.60.20variants.20of.20read.2Fwrite.20to.20.60unix.3A.3Afs.3A.3AFileExt.60.html">Adding `&amp;self` variants of read/write to `unix::fs::FileExt`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="205212521"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20%60%26self%60%20variants%20of%20read/write%20to%20%60unix%3A%3Afs%3A%3AFileExt%60/near/205212521" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> John Millikin <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20.60.26self.60.20variants.20of.20read.2Fwrite.20to.20.60unix.3A.3Afs.3A.3AFileExt.60.html#205212521">(Jul 28 2020 at 09:04)</a>:</h4>
<p>Hello -- this is my first time using Zulip, please redirect if I'm in the wrong place.</p>
<p>The default <code>std::fs::File</code> functions <code>read()</code> and <code>write()</code> come from <code>std::io::</code> traits, which are <code>&amp;mut self</code>. This is unnecessarily strict on UNIX platforms, which allow concurrent read/write to the same file descriptor. The internal implementation in <code>library/std/src/sys/unix/fs.rs</code> uses <code>&amp;self</code>, so I wonder if it might be possible to expose this capability in the <code>std::os::unix::fs::FileExt</code> trait. I think I could put together a PR, but I'm reluctant to go through the FCP and stabilization requirements documented in the contribution guide. Is there a low-overhead contribution process for stdlib additions that are unlikely to have compatibility problems?</p>



<a name="205212768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20%60%26self%60%20variants%20of%20read/write%20to%20%60unix%3A%3Afs%3A%3AFileExt%60/near/205212768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> John Millikin <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20.60.26self.60.20variants.20of.20read.2Fwrite.20to.20.60unix.3A.3Afs.3A.3AFileExt.60.html#205212768">(Jul 28 2020 at 09:08)</a>:</h4>
<p>My main concern is that these <code>*Ext</code> traits appear to be user-implementable, so I don't know if adding new functions counts as a backwards-incompatible change.</p>



<a name="205212848"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20%60%26self%60%20variants%20of%20read/write%20to%20%60unix%3A%3Afs%3A%3AFileExt%60/near/205212848" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20.60.26self.60.20variants.20of.20read.2Fwrite.20to.20.60unix.3A.3Afs.3A.3AFileExt.60.html#205212848">(Jul 28 2020 at 09:09)</a>:</h4>
<p>I don't think this is necessary as <code>&amp;File</code> already implements<code>Read</code> and <code>Write</code></p>



<a name="205212922"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20%60%26self%60%20variants%20of%20read/write%20to%20%60unix%3A%3Afs%3A%3AFileExt%60/near/205212922" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20.60.26self.60.20variants.20of.20read.2Fwrite.20to.20.60unix.3A.3Afs.3A.3AFileExt.60.html#205212922">(Jul 28 2020 at 09:10)</a>:</h4>
<p>And getting an <code>&amp;mut &amp;File</code> is always possible as<code>&amp;File</code> is <code>Copy</code></p>



<a name="205213094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20%60%26self%60%20variants%20of%20read/write%20to%20%60unix%3A%3Afs%3A%3AFileExt%60/near/205213094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> John Millikin <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20.60.26self.60.20variants.20of.20read.2Fwrite.20to.20.60unix.3A.3Afs.3A.3AFileExt.60.html#205213094">(Jul 28 2020 at 09:12)</a>:</h4>
<p>I don't understand what you mean, sorry. Given a <code>fp: &amp;std::fs::File</code>, how would you recommend I use that reference to write data to it?</p>



<a name="205213143"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20%60%26self%60%20variants%20of%20read/write%20to%20%60unix%3A%3Afs%3A%3AFileExt%60/near/205213143" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> John Millikin <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20.60.26self.60.20variants.20of.20read.2Fwrite.20to.20.60unix.3A.3Afs.3A.3AFileExt.60.html#205213143">(Jul 28 2020 at 09:13)</a>:</h4>
<p>On UNIX the type is semantically <code>fn write(&amp;self, buf: &amp;[u8})</code> -- the <code>&amp;mut self</code> comes from the <code>std::fs::File</code> wrapper.</p>



<a name="205213217"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20%60%26self%60%20variants%20of%20read/write%20to%20%60unix%3A%3Afs%3A%3AFileExt%60/near/205213217" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20.60.26self.60.20variants.20of.20read.2Fwrite.20to.20.60unix.3A.3Afs.3A.3AFileExt.60.html#205213217">(Jul 28 2020 at 09:14)</a>:</h4>
<div class="codehilite"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">fs</span>::<span class="n">File</span><span class="p">;</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span><span class="n">std</span>::<span class="n">io</span>::<span class="p">{</span><span class="n">Read</span><span class="p">,</span><span class="w"> </span><span class="n">Write</span><span class="p">};</span><span class="w"></span>

<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">use_me</span><span class="p">(</span><span class="k">mut</span><span class="w"> </span><span class="n">v</span>: <span class="kp">&amp;</span><span class="nc">File</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="nb">String</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">s</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nb">String</span>::<span class="n">new</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="n">v</span><span class="p">.</span><span class="n">read_to_string</span><span class="p">(</span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="n">s</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">v</span><span class="p">.</span><span class="n">write</span><span class="p">(</span><span class="n">s</span><span class="p">.</span><span class="n">as_bytes</span><span class="p">());</span><span class="w"></span>
<span class="w">    </span><span class="n">s</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="205213260"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20%60%26self%60%20variants%20of%20read/write%20to%20%60unix%3A%3Afs%3A%3AFileExt%60/near/205213260" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20.60.26self.60.20variants.20of.20read.2Fwrite.20to.20.60unix.3A.3Afs.3A.3AFileExt.60.html#205213260">(Jul 28 2020 at 09:14)</a>:</h4>
<p>See here <a href="https://doc.rust-lang.org/nightly/std/fs/struct.File.html#impl-Read-1">https://doc.rust-lang.org/nightly/std/fs/struct.File.html#impl-Read-1</a></p>



<a name="205213272"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20%60%26self%60%20variants%20of%20read/write%20to%20%60unix%3A%3Afs%3A%3AFileExt%60/near/205213272" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20.60.26self.60.20variants.20of.20read.2Fwrite.20to.20.60unix.3A.3Afs.3A.3AFileExt.60.html#205213272">(Jul 28 2020 at 09:15)</a>:</h4>
<p>and</p>
<blockquote>
<p>Note that, although read and write methods require a &amp;mut File, because of the interfaces for Read and Write, the holder of a &amp;File can still modify the file, either through methods that take &amp;File or by retrieving the underlying OS object and modifying the file that way. Additionally, many operating systems allow concurrent modification of files by different processes. Avoid assuming that holding a &amp;File means that the file will not change.</p>
</blockquote>



<a name="205213382"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20%60%26self%60%20variants%20of%20read/write%20to%20%60unix%3A%3Afs%3A%3AFileExt%60/near/205213382" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> John Millikin <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20.60.26self.60.20variants.20of.20read.2Fwrite.20to.20.60unix.3A.3Afs.3A.3AFileExt.60.html#205213382">(Jul 28 2020 at 09:16)</a>:</h4>
<p>Ah, I see, so the type becomes <code>&amp;mut &amp;File</code>, and the read/write functions are available through that?</p>



<a name="205213387"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20%60%26self%60%20variants%20of%20read/write%20to%20%60unix%3A%3Afs%3A%3AFileExt%60/near/205213387" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20.60.26self.60.20variants.20of.20read.2Fwrite.20to.20.60unix.3A.3Afs.3A.3AFileExt.60.html#205213387">(Jul 28 2020 at 09:16)</a>:</h4>
<p>yeah <span aria-label="thumbs up" class="emoji emoji-1f44d" role="img" title="thumbs up">:thumbs_up:</span></p>



<a name="205213414"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20%60%26self%60%20variants%20of%20read/write%20to%20%60unix%3A%3Afs%3A%3AFileExt%60/near/205213414" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> John Millikin <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20.60.26self.60.20variants.20of.20read.2Fwrite.20to.20.60unix.3A.3Afs.3A.3AFileExt.60.html#205213414">(Jul 28 2020 at 09:17)</a>:</h4>
<p>I understand, that does make sense, thank you! You can probably tell I'm fairly new to this, so I didn't realize a double-pointer could be used in that way.</p>



<a name="205213605"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20%60%26self%60%20variants%20of%20read/write%20to%20%60unix%3A%3Afs%3A%3AFileExt%60/near/205213605" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20.60.26self.60.20variants.20of.20read.2Fwrite.20to.20.60unix.3A.3Afs.3A.3AFileExt.60.html#205213605">(Jul 28 2020 at 09:19)</a>:</h4>
<p>No worries, rust has some abstractions which (while often clever), aren't really intuitive if you don't already know them. So it's fine to miss them and ask questions</p>



<a name="205213635"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20%60%26self%60%20variants%20of%20read/write%20to%20%60unix%3A%3Afs%3A%3AFileExt%60/near/205213635" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20.60.26self.60.20variants.20of.20read.2Fwrite.20to.20.60unix.3A.3Afs.3A.3AFileExt.60.html#205213635">(Jul 28 2020 at 09:19)</a>:</h4>
<blockquote>
<p>rust has some abstractions</p>
</blockquote>
<p>from what I have seen, most languages do</p>



<a name="205238668"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20%60%26self%60%20variants%20of%20read/write%20to%20%60unix%3A%3Afs%3A%3AFileExt%60/near/205238668" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20.60.26self.60.20variants.20of.20read.2Fwrite.20to.20.60unix.3A.3Afs.3A.3AFileExt.60.html#205238668">(Jul 28 2020 at 14:00)</a>:</h4>
<p>See also <a href="https://stackoverflow.com/q/31503488/155423">Why is it possible to implement Read on an immutable reference to File?</a>.</p>



<a name="206544383"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Adding%20%60%26self%60%20variants%20of%20read/write%20to%20%60unix%3A%3Afs%3A%3AFileExt%60/near/206544383" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Adding.20.60.26self.60.20variants.20of.20read.2Fwrite.20to.20.60unix.3A.3Afs.3A.3AFileExt.60.html#206544383">(Aug 11 2020 at 06:39)</a>:</h4>
<p><span class="user-mention" data-user-id="325706">@John Millikin</span> It's worth finding out early that a reference is not always necessarily a pointer. It can be, but Rust can also optimize that away.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>